package com.zscat.mallplus.service.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.entity.vo.pms.PmsProductFootprintVo;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.entity.PmsProductFootprint;
import com.zscat.mallplus.pms.mapper.PmsProductFootprintMapper;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.service.pms.service.IPmsFootprintService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ZuRongTang
 * @Date 2020/3/2
 **/

@Service
public class PmsFootprintServiceImpl extends ServiceImpl<PmsProductFootprintMapper, PmsProductFootprint> implements IPmsFootprintService {

    @Resource
    private PmsProductMapper pmsProductMapper;

    @Resource
    private PmsProductFootprintMapper pmsProductFootprintMapper;


    @Override
    public CommonResult addFootprint(Long memberId, Long productId) {
        CommonResult commonResult = new CommonResult();

        PmsProductFootprint pmsProductFootprint = new PmsProductFootprint();

        PmsProduct pmsProduct = pmsProductMapper.selectById(productId);

        if (pmsProduct == null) {
            return commonResult.failed("商品不存在");
        }


        pmsProductFootprint.setState(0);
        pmsProductFootprint.setMemberId(memberId);
        pmsProductFootprint.setProductId(productId);
        pmsProductFootprint.setPrice(pmsProduct.getPrice());
        pmsProductFootprint.setProductName(pmsProduct.getName());
        pmsProductFootprint.setPromotionPrice(pmsProduct.getPromotionPrice());

        //图片取第一张、 第一张如果是视频,则取第二张
        String albumPics = pmsProduct.getAlbumPics();

        if (!StringUtils.isEmpty(albumPics)) {

            String[] split = albumPics.split(",");

            String img = split[0];

            //截取后缀、判断是否是mp4
            String suffix = img.substring(img.lastIndexOf(".") + 1);

            if ("mp4".equals(suffix)) {
                pmsProductFootprint.setProductImg(split[1]);
            } else {
                pmsProductFootprint.setProductImg(split[0]);
            }
        } else {
            pmsProductFootprint.setProductImg(null);
        }

        pmsProductFootprint.setAddTime(System.currentTimeMillis());
        pmsProductFootprint.setCreateTime(System.currentTimeMillis());
        pmsProductFootprint.setUpdateTime(System.currentTimeMillis());

        pmsProductFootprintMapper.insert(pmsProductFootprint);

        return commonResult.success();

    }

    @Override
    public PageInfo<PmsProductFootprintVo> getMyFootprintList(Long memberId, Integer pageSize, Integer pageIndex) throws ParseException {

        List<PmsProductFootprintVo> result = new ArrayList<>();

        IPage<PmsProductFootprint> member_id = findMyFootrintList("member_id", memberId, pageIndex.longValue(), pageSize.longValue());

        List<PmsProductFootprint> records = member_id.getRecords();

        if (records.size() > 0) {

            for (PmsProductFootprint pmsProductFootprint : records) {
                PmsProductFootprintVo pmsProductFootprintVo = new PmsProductFootprintVo();

                Long addTime = pmsProductFootprint.getAddTime();

                String time = handleDate(addTime);

                BeanUtils.copyProperties(pmsProductFootprint, pmsProductFootprintVo);

                if ("1".equals(time)) {
                    pmsProductFootprintVo.setTimeType(0);
                } else if ("2".equals(time)) {
                    pmsProductFootprintVo.setTimeType(1);
                } else {
                    pmsProductFootprintVo.setTimeType(2);
                }

                result.add(pmsProductFootprintVo);
            }

            return new PageInfo<>(pageIndex, pageSize, result);
        } else {
            return new PageInfo<>(pageIndex, pageSize, result);
        }
    }

    @Override
    public void removeMyFootprint(String id) {
        String[] split = id.split(",");

        for (String s : split) {
            pmsProductFootprintMapper.deleteById(Long.valueOf(s));
        }


    }

    private static String handleDate(long time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(time);
        Date old = sdf.parse(sdf.format(date));
        Date now = sdf.parse(sdf.format(new Date()));
        long oldTime = old.getTime();
        long nowTime = now.getTime();

        long day = (nowTime - oldTime) / (24 * 60 * 60 * 1000);

        if (day < 1) {  //今天
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return "1";
        } else if (day == 1) {     //昨天
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return "2";
        } else {    //可依次类推
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return "3";
        }
    }

    private IPage<PmsProductFootprint> findMyFootrintList(String column, Object value, Long value1, Long value2) {
        QueryWrapper<PmsProductFootprint> queryWrapper = new QueryWrapper<>();

        IPage<PmsProductFootprint> page = new Page<>();

        page.setSize(value2);
        page.setCurrent(value1);


        queryWrapper.eq(column, value).orderByDesc("create_time");
        return pmsProductFootprintMapper.selectPage(page, queryWrapper);
    }
}
